/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-8-24         yellowlgx
 *
 */

package com.skyworth.ui.api;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

/**
 * @ClassName SkyImgLoadingView
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2015-8-24
 * @version TODO (write something)
 */
public class SkyImgLoadingView extends ImageView
{
    private static int ANIM_ROTATE = 1;
    private static int ANIM_FRAME = 2;

    private int animType = -1;

    private Context mContext;
    private Animation rotateAnim; // 旋转动画
    private AnimationDrawable framAnim;

    public SkyImgLoadingView(Context context)
    {
        super(context);
        mContext = context;
        this.setVisibility(View.INVISIBLE);
    }

    public SkyImgLoadingView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        this.setVisibility(View.INVISIBLE);
    }

    public SkyImgLoadingView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        this.setVisibility(View.INVISIBLE);
    }

    /**
     * 概述：初始化旋转动画
     * 
     * @param drawable
     *            需要左旋转的图标，Drawable对象
     * @date 2015-2-2
     */
    public void initRotateAnim(Drawable drawable)
    {
        animType = ANIM_ROTATE;
        rotateAnim = new RotateAnimation(0.0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnim.setDuration(1000);
        rotateAnim.setInterpolator(new LinearInterpolator());
        rotateAnim.setRepeatMode(Animation.RESTART);
        rotateAnim.setRepeatCount(Animation.INFINITE);
        if (drawable != null)
        {
            this.setImageDrawable(drawable);
        } else
        {
            // this.setImageResource(R.drawable.loading_young);
        }
    }

    /**
     * 概述：初始化帧动画
     * 
     * @param frames
     *            帧动画执行的图集，子元素为Drawable对象,传null则用commonUI默认的图，时间随便传值
     * @param duration
     *            图集之间切换的时间
     * @date 2015-2-2
     */
    public void initFrameAnim(List<Drawable> frames, int duration)
    {
        animType = ANIM_FRAME;
        if (frames != null && frames.size() > 1)
        {
            framAnim = new AnimationDrawable();
            for (int i = 0; i < frames.size(); i++)
            {
                if (frames.get(i) != null)
                    framAnim.addFrame(frames.get(i), duration);
            }
            framAnim.setOneShot(false);
            this.setImageDrawable(framAnim);
        } else
        {
            // this.setBackgroundResource(R.drawable.ui_sdk_img_loading_default);
            // framAnim = (AnimationDrawable) this.getBackground();
        }
    }

    /**
     * 概述：启动loading动画
     * 
     * @date 2015-2-2
     */
    public void startAnim()
    {
        if (this.getVisibility() != View.VISIBLE)
            this.setVisibility(View.VISIBLE);
        if (animType == ANIM_ROTATE)
        {
            this.startAnimation(rotateAnim);
        } else if (animType == ANIM_FRAME)
        {
            if (framAnim != null && !framAnim.isRunning())
            {
                framAnim.start();
            }
        }
    }

    /**
     * 概述：停止loading动画
     * 
     * @date 2015-2-2
     */
    public void stopAnim()
    {
        if (animType == ANIM_FRAME)
        {
            if (framAnim != null && framAnim.isRunning())
            {
                framAnim.stop();
            }
        }
        this.clearAnimation();
        this.setVisibility(View.INVISIBLE);
    }

}
